package uk.co.lunagroup.xmloperations;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class AddInfoToXMLFile {

	public static void addInfo(final File file, final FileList fileInfo) {

		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(file);

			// Get the root element
			Node rootElement = doc.getFirstChild();

			// Get the staff element , it may not working if tag has spaces, or
			// whatever weird characters in front...it's better to use
			// getElementsByTagName() to get it directly.
			Node row = rootElement.getFirstChild();

			// create new node DocumentInfo
			Element docInfo = doc.createElement("DocumentInfo");
			rootElement.insertBefore(docInfo, row);

			// create tendrNr element
			Element tenderNr = doc.createElement("TenderNr");
			tenderNr.appendChild(doc.createTextNode(fileInfo.getTender()));
			docInfo.appendChild(tenderNr);

			// create revision element
			Element revision = doc.createElement("Revision");
			revision.appendChild(doc.createTextNode(fileInfo.getRevision()));
			docInfo.appendChild(revision);

			// create tendrName element
			Element tenderName = doc.createElement("Tender");
			tenderName
					.appendChild(doc.createTextNode(fileInfo.getTenderName()));
			docInfo.appendChild(tenderName);

			// create XMLFilePath element
			Element XMLFilePath = doc.createElement("XMLFilePath");
			XMLFilePath.appendChild(doc.createTextNode(file.getAbsolutePath()));
			docInfo.appendChild(XMLFilePath);

			// create XMLServerPath element
			Element XMLServerPath = doc.createElement("XMLServerPath");
			XMLServerPath.appendChild(doc.createTextNode(fileInfo
					.getServerXmlFilePath()));
			docInfo.appendChild(XMLServerPath);

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			// set some options on the transformer. makes document look nice
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
			transformer
					.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount", "2");

			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(file);
			transformer.transform(source, result);

			System.out.println("Done");

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (SAXException sae) {
			sae.printStackTrace();
		}
	}
}